package qeug.shsh.opplugin;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import qeug.shsh.bpm.Factory.ThirdServiceFactory;
import qeug.shsh.bpm.service.BPMSrevice;
import qeug.shsh.bpm.service.BPMSreviceV2;
import qeug.shsh.helper.FacadeHelper;

import java.util.HashMap;
import java.util.List;

/**
 * @projectName: qeug-cosmic-root-project
 * @package: qeug.shsh.bpm.service.impl
 * @className: BPMServiceImpl
 * @author: wenyx
 * @modifier: xuhh
 * @description: BPM提交插件
 * @date: 2023/11/3/16:21
 * @version: 2.0
 */
public class ApplybillServicePlugin extends AbstractOperationServicePlugIn {


    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        String operationKey = e.getOperationKey();
        if ("submit".equals(operationKey)) {
            //调用webService接口
            DynamicObject[] dataEntities = e.getDataEntities();
            String entityName = null;
            if (dataEntities.length > 0) {
                entityName = dataEntities[0].getDataEntityType().getName();
            }
            List<ExtendedDataEntity> selectedRows = e.getSelectedRows();
            for (ExtendedDataEntity selectedRow : selectedRows) {
                DynamicObject bosObject = null;
                try {
                    Object id = selectedRow.getValue("id");
                    QFilter[] a = new QFilter[]{new QFilter("id", "=", id)};
                    BPMSrevice bpmSrevice = ThirdServiceFactory.createBPMSrevice();
                    BPMSreviceV2 bpmSreviceV2 = ThirdServiceFactory.createBPMV2Srevice();
                    if (entityName != null) {
                        switch (entityName) {
                            //旧流程
                            case "er_tripreimbursebill"://差旅报销单
                                bosObject = BusinessDataServiceHelper.loadSingle(entityName, a);
                                bpmSrevice.tripReimburse(bosObject);
                                break;
                            case "er_dailyreimbursebill"://费用报销单
                                bosObject = BusinessDataServiceHelper.loadSingle(entityName, a);
                                bpmSrevice.dailyReimburse(bosObject);
                                break;
                            case "bgm_adjustbill"://预算调整单
                                bosObject = BusinessDataServiceHelper.loadSingle(entityName, a);
                                bpmSrevice.adjustBill(bosObject);
                                break;
                            //新流程
                            case "er_publicreimbursebill"://付款申请单(对公改造)
                                bosObject = BusinessDataServiceHelper.loadSingle(entityName, a);
                                bpmSreviceV2.publicReimburseBill(bosObject);
                                break;
                            case "qeug_fundplan"://资金计划
                            case "qeug_fundadjust"://资金计划调整
                                bosObject = BusinessDataServiceHelper.loadSingle(entityName, a);
                                bpmSreviceV2.billOrgTpl(bosObject);
                                break;
                            case "er_dailyloanbill"://借款单
                                bosObject = BusinessDataServiceHelper.loadSingle(entityName, a);
                                bpmSreviceV2.dailyLoanBill(bosObject);
                                break;
                            default:
                                throw new IllegalArgumentException("Invalid entityName: " + entityName);
                        }
                    }
                } catch (Exception ex) {
                    OperationServiceHelper.executeOperate("unsubmit", entityName, new DynamicObject[]{bosObject}, OperateOption.create());//撤销推送失败的单据
                    HashMap<String, Object> log = new HashMap<>();
                    log.put("responsebody", ex);
                    FacadeHelper.apiLog(log);
                    throw new RuntimeException(ex);
                }
            }
        }
    }
}
